﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Xiami.Helper.Model;
using AMing.Extension.Net40;

namespace MusicForMvc3.Method.Data
{
    public class GetXiami : Cache.CacheBase<GetXiami, List<Model.music>>
    {
        [Cache.CacheKey(ValueMD5 = true)]
        public string Url { get; set; }


        public override string GetData()
        {
            return base.GetData();
        }

        public override List<Model.music> GetObject()
        {
            var url = System.Web.HttpUtility.UrlDecode(this.Url);
            Method.XiamiDecode xiamiHelper = new Method.XiamiDecode();
            var list = xiamiHelper.GetTrackData(url);
            var music_list = GetList(list);

            return music_list;
        }


        private List<Model.music> GetList(TrackListInfo xiamiList)
        {
            List<Model.music> list = new List<Model.music>();
            foreach (var item in xiamiList.TrackList)
            {
                TimeSpan timespan = new TimeSpan(0, 0, item.Length);
                var music = new Model.music()
                {
                    artist = item.artist,
                    mp3 = item.MP3Url,
                    cover = item.Pic_2,
                    title = item.Title,
                    rating = 5,
                    duration = "{0}:{1:00}".ToFromat(timespan.Minutes, timespan.Seconds)
                };
                list.Add(music);
            }

            return list;
        }
    }
}